#include <iostream>
#include <string>

using namespace std;

#define MAX 1000

struct Person {
    string m_Name;
    int m_Age;
    int m_Sex;
    string m_Phone;
    string m_Addr;
};

struct AddressBook {
    Person personArray[MAX];
    int m_Size;
};

void continueNext() {
    cout << "请按任意键继续" << endl;
    cin.get();
    cin.get();
    system("clear");
}

void showMenu() {
    cout << "*************************" << endl;
    cout << "***** 1. 添加联系人 *****" << endl;
    cout << "***** 2. 显示联系人 *****" << endl;
    cout << "***** 3. 删除联系人 *****" << endl;
    cout << "***** 4. 查找联系人 *****" << endl;
    cout << "***** 5. 修改联系人 *****" << endl;
    cout << "***** 6. 清空联系人 *****" << endl;
    cout << "***** 0. 退出通讯录 *****" << endl;
    cout << "*************************" << endl;
}

void inputPerson(Person &p) {
    string name;
    cout << "请输入姓名" << endl;
    cin >> name;
    p.m_Name = name;
    cout << "请输入年龄" << endl;
    cin >> p.m_Age;
    cout << "请输入性别" << endl;
    cout << "1 -- 男 2 -- 女" << endl;
    int sex = 0;
    while (true)
    {
        cin >> sex;
        if (sex == 1 || sex == 2) {
            p.m_Sex = sex;
            break;
        }
        cout << "输入有误，请重新输入" << endl;
    }
    cout << "请输入电话" << endl;
    cin >> p.m_Phone;
    cout << "请输入地址" << endl;
    cin >> p.m_Addr;
}

void addPerson(AddressBook* ab) {
    if (ab->m_Size == MAX) {
        cout << "通讯录已满，无法添加" << endl;
        return;
    }
    else {
        inputPerson(ab->personArray[ab->m_Size]);
        ab->m_Size++;
        cout << "添加成功" << endl;
        continueNext();
    }
}

void showPerson(const Person person) {
    cout << "姓名：" << person.m_Name << "\t"
         << " 年龄：" << person.m_Age << "\t"
         << " 性别：" << (person.m_Sex == 1 ? "男" : "女") << "\t"
         << " 电话：" << person.m_Phone << "\t"
         << " 地址：" << person.m_Addr << "\t"
         << endl;
}

void findPerson(const AddressBook &ab) {
    if (ab.m_Size == 0) {
        cout << "通讯录为空" << endl;
    }
    else {
        for (int i = 0; i < ab.m_Size; i++) {
            showPerson(ab.personArray[i]);
        }
    }
    continueNext();
}

int isExist(AddressBook* ab, string name) {
    for (int i = 0; i < ab->m_Size; i++) {
        if (ab->personArray[i].m_Name == name) {
            return i;
        }
    }
    return -1;
}

void deletePerson(AddressBook* ab) {
    if (ab->m_Size == 0) {
        cout << "通讯录为空" << endl;
    }
    else {
        string name;
        cout << "请输入要删除的联系人姓名" << endl;
        cin >> name;
        int index = -1; //记录要删除的联系人下标
        if ((index = isExist(ab, name)) != -1) {
            for (int j = index; j < ab->m_Size - 1; j++) {
                ab->personArray[j] = ab->personArray[j + 1];
            }
            ab->m_Size--;
            cout << "删除成功" << endl;
        }
        else {
            cout << "未找到该联系人" << endl;
        }
    }
    continueNext();
}

void findPerson(AddressBook* ab) {
    if (ab->m_Size == 0) {
        cout << "通讯录为空" << endl;
    }
    else {
        string name;
        cout << "请输入要查找的联系人姓名" << endl;
        cin >> name;
        int index = -1; //记录要查找的联系人下标
        if ((index = isExist(ab, name)) != -1) {
            showPerson(ab->personArray[index]);
        }
        else {
            cout << "未找到该联系人" << endl;
        }
    }
    continueNext();
}

void modifyPerson(AddressBook* ab) {
    if (ab->m_Size == 0) {
        cout << "通讯录为空" << endl;
    }
    else {
        string name;
        cout << "请输入要修改的联系人姓名" << endl;
        cin >> name;
        int index = -1; //记录要修改的联系人下标
        if ((index = isExist(ab, name)) != -1) {
            Person p;
            inputPerson(p);
            ab->personArray[index] = p;
            cout << "修改成功" << endl;
        }
        else {
            cout << "未找到该联系人" << endl;
        }
    }
    continueNext();
}

void clearAll(AddressBook* ab) {
    ab->m_Size = 0;
    cout << "清空成功" << endl;
    continueNext();
}

int main() {

    struct AddressBook ab;
    ab.m_Size = 0;

    int select = 0; //用户选择
    
    while (true)
    {    
        showMenu();

        cin >> select;
        switch (select) {
            case 1:
                cout << "添加联系人" << endl;
                addPerson(&ab);
                break;
            case 2:
                cout << "显示联系人" << endl;
                findPerson(ab);
                break;
            case 3:
                cout << "删除联系人" << endl;
                deletePerson(&ab);
                break;
            case 4:
                cout << "查找联系人" << endl;
                findPerson(&ab);
                break;
            case 5:
                cout << "修改联系人" << endl;
                modifyPerson(&ab);
                break;
            case 6:
                cout << "清空联系人" << endl;
                clearAll(&ab);
                break;
            case 0:
                cout << "退出通讯录" << endl;
                cin.get();
                return 0;
                break;
            default:
                cout << "输入有误，请重新输入" << endl;
                break;
        }
    }
    
    return 0;
}